package com.zzyl.nursing.service.impl;


import java.util.List;
import java.util.stream.Collectors;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-29
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        //根据护理计划id查询项目
        List<NursingProjectPlan> projectPlans = nursingProjectPlanMapper.selectNursingProjectPlanListById(id);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;

    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param planDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto planDto) {
        // 1.新增护理计划
        NursingPlan nursingPlan = new NursingPlan();
//        nursingPlan.setSortNo(planDto.getSortNo());
//        nursingPlan.setPlanName(planDto.getPlanName());
//        nursingPlan.setStatus(planDto.getStatus());
        BeanUtils.copyProperties(planDto, nursingPlan); // 属性名 数据类型要一致
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        System.out.println(nursingPlan.getId());
        // 2.新增护理项目集合
        List<NursingProjectPlanDto> projectPlans = planDto.getProjectPlans();
        List<NursingProjectPlan> projectPlan = projectPlans.stream().map(dto -> {
            NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlan.setProjectId(Integer.parseInt(dto.getProjectId()));
            nursingProjectPlan.setExecuteTime(dto.getExecuteTime());
            nursingProjectPlan.setExecuteCycle(dto.getExecuteCycle().toString());
            nursingProjectPlan.setExecuteFrequency(dto.getExecuteFrequency());
            nursingProjectPlan.setCreateTime(DateUtils.getNowDate());
            return nursingProjectPlan;
        }).collect(Collectors.toList());
        nursingProjectPlanMapper.insertBatch(projectPlan);
//        nursingPlan.setCreateTime(DateUtils.getNowDate());
//        return nursingPlanMapper.insertNursingPlan(nursingPlan);
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param planDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto planDto) {
        // 1.修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(planDto, nursingPlan);
        nursingPlan.setUpdateTime(DateUtils.getNowDate());
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        // 2.修改护理项目
        if (planDto.getProjectPlans() != null && planDto.getProjectPlans().size() > 0) {
            // 2.1获取护理计划id
            Integer id = nursingPlan.getId();
            // 先删除全部护理项目
            nursingProjectPlanMapper.deleteNursingProjectPlanListById(id);
            // 再新增护理项目
            List<NursingProjectPlanDto> projectPlans = planDto.getProjectPlans();
            List<NursingProjectPlan> projectPlan = projectPlans.stream().map(dto -> {
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                nursingProjectPlan.setPlanId(nursingPlan.getId());
                nursingProjectPlan.setProjectId(Integer.parseInt(dto.getProjectId()));
                nursingProjectPlan.setExecuteTime(dto.getExecuteTime());
                nursingProjectPlan.setExecuteCycle(dto.getExecuteCycle().toString());
                nursingProjectPlan.setExecuteFrequency(dto.getExecuteFrequency());
                nursingProjectPlan.setCreateTime(DateUtils.getNowDate());
                return nursingProjectPlan;
            }).collect(Collectors.toList());
            nursingProjectPlanMapper.insertBatch(projectPlan);
        }
        //别管项目列表是否为空，都要修改护理计划
        return nursingPlanMapper.updateNursingPlan(nursingPlan);


//        nursingPlan.setUpdateTime(DateUtils.getNowDate());
//        return nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanById(Integer id) {
        try {
            // 删除护理计划
            nursingPlanMapper.deleteNursingPlanById(id);
            // 删除该计划下的护理项目
            nursingProjectPlanMapper.deleteNursingProjectPlanListById(id);
            return 1;
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询所有启用的护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> selectNursingPlanAll() {
       return nursingPlanMapper.selectNursingPlanAll();
    }
}
